﻿#ifndef NET_H
#define NET_H

#include "../sys_h/DATATYPE.h"
#include "../sys_h/DATATYPE_S.h"
#include "../sys_h/NSA3000TBF.h"
#include "../db_h/DBDEFINE.h"

#pragma pack(1)

#define MAX_NET_MSG_SEND_COUNT                             10
#define MAX_NET_MSG_READ_COUNT                             10

#define LOCAL_GPS_BROAD_MIX_TIME                           15
#define LOCAL_GPS_BROAD_MAX_TIME                           45
#define LOCAL_GPS_BROAD_MIX_SEC_TIME                       200
#define LOCAL_GPS_BROAD_MAX_SEC_TIME                       700
#define LOCAL_GPS_BROAD_PLUSE_TIME                         60
#define LOCAL_GPS_BROAD_PRECISION_COUNT                    5
#define LOCAL_GPS_BROAD_PRECI_MINI_COUNT                   500
#define NET_MAX_NODE_LISTEN                                5

#define NSA3000T_UDP_MSG_SEND_PLUSE_BY_IN_SYS               5   
#define NSA3000T_UDP_MSG_SEND_PLUSE_BY_OUT_SYS              20   
#define NSA3000T_UDP_MSG_SEND_MAX_COUNT                     3
#define NSA3000T_UDP_MSG_SEND_OUT_TIME_IN_SYS               NSA3000T_UDP_MSG_SEND_PLUSE_BY_IN_SYS*NSA3000T_UDP_MSG_SEND_MAX_COUNT
#define NSA3000T_UDP_MSG_SEND_OUT_TIME_OUT_SYS              NSA3000T_UDP_MSG_SEND_PLUSE_BY_OUT_SYS*NSA3000T_UDP_MSG_SEND_MAX_COUNT
#define NSA3000T_CONN_DELAY                                 30
#define NSA3000T_NODE_MSG_MAX_DELAY                         5*60   
#define NSA3000T_TCP_MSG_SEND_START_TIME                    5
#define NSA3000T_TCP_MSG_SEND_PLUSE_TIME                    20
#define NSA3000T_TCP_MSG_SEND_MAX_COUNT                     3
#define NSA3000T_TCP_MSG_SEND_OUT_TIME                      NSA3000T_TCP_MSG_SEND_PLUSE_TIME*NSA3000T_TCP_MSG_SEND_MAX_COUNT
#define NSA3000T_QUIT_OUT_TIME                              NSA3000T_UDP_MSG_SEND_PLUSE_BY_OUT_SYS

#define CHECK_NET_STATE_PLUSE                              30

#define NET_LINK_PORT_SERVER_LISTEN_TCP_PORT               5001
#define NET_LINK_PORT_SERVER_LISTEN_UDP_PORT               5002
#define NET_FILE_PORT_SERVER_LISTEN_TCP_PORT               5003

#define ORDER_NUM_C1_INCEPT                                0x01000000
#define ORDER_NUM_C2_INCEPT                                0x0
#define ORDER_NUM_C3C4_INCEPT                              0x0001
#define ORDER_NUM_INCEPT                                   ORDER_NUM_C1_INCEPT+ORDER_NUM_C2_INCEPT+ORDER_NUM_C3C4_INCEPT
#define SYS_NET_COUNT                                      2
#define DATA_SOURCE_NET_0                                  0x0
#define DATA_SOURCE_NET_A                                  0x1
#define DATA_SOURCE_NET_B                                  0x2
#define NODE_RUNWAY_LOCAL						           0		//本地模式
#define NODE_RUNWAY_ONE_NET						           1		//单网模式
#define NODE_RUNWAY_TWO_NET						           2		//双网模式
#define NET_MSG_HEAD_55                                    0x55
#define NET_MSG_HEAD_AA                                    0xAA
#define NET_MSG_TAIL                                       0x16
#define NSA3000T_NET_MSG_MIX_CELL                          1024*sizeof(XCHAR)
#define NSA3000T_TRANS_SINGLE_P_P_NET_MSG_COUNT            5
#define NSA3000T_TRANS_SINGLE_B_D_NET_MSG_COUNT            5
#define NSA3000T_TRANS_SINGLE_NET_MSG_COUNT                NSA3000T_TRANS_SINGLE_P_P_NET_MSG_COUNT+NSA3000T_TRANS_SINGLE_B_D_NET_MSG_COUNT
#define NODE_PING_PLUSE                                    10
#define NODE_MSG_MAX_DELAY                                 5*60
#define NODE_TCP_MSG_SEND_ERR_MAX_COUNT                    2
#define NSA3000T_NODE_B_D_MSG_COUNT                        1024
#define NSA3000T_NET_MSG_B_D_TCP_COUNT                     (XUSHORT)(NODE_MAX_COUNT*NSA3000T_NODE_B_D_MSG_COUNT) 

#define NSA3000T_NET_MSG_TYPE_NONE                         0X0
#define NSA3000T_NET_MSG_TYPE_P_TO_P_NORMAL                0X1
#define NSA3000T_NET_MSG_TYPE_P_TO_P_HIGH                  0X2
#define NSA3000T_NET_MSG_TYPE_P_P_MAX                      0X80
#define NSA3000T_NET_MSG_TYPE_BROAD_LOACL_NODE             0X1+NSA3000T_NET_MSG_TYPE_P_P_MAX         //广播消息,范围:当前节点
#define NSA3000T_NET_MSG_TYPE_BROAD_LOACL_NET              0X2+NSA3000T_NET_MSG_TYPE_P_P_MAX         //广播消息,范围:闭环系统
#define NSA3000T_NET_MSG_TYPE_BROAD_LOACL_SYS              0XF0                                      //广播消息,范围:所有节点

//完整网络信息体
typedef struct _NSA3000T_NET_MSG
{
	XUCHAR                                                 net_msg_head1;                                                    //网络报文头
	XUSHORT                                                net_msg_len;                                                      //网络消息长度
	XUINT                                                  node_random_num;                                                  //节点随机码
	XUCHAR                                                 net_msg_type;                                                     //消息属性
	XUINT                                                  net_err_num;                                                      //网络错误字
	XUCHAR                                                 net_msg_head2;                                                    //网络报文头
	XUSHORT                                                s_node_id;                                                        //发送节点
	XUSHORT                                                s_pro_id;                                                         //发送进程
	XUSHORT                                                e_node_id;                                                        //目的节点
	XUSHORT                                                e_pro_id;                                                         //目的进程
	XUCHAR                                                 about_frame;                                                      //关于包
	XUSHORT                                                append_data;                                                      //数据附加
	XUINT                                                  order_num;                                                        //命令字
	XCHAR                                                 *pnet_buf;                                                         //数据指针地址 NSA3000T_NET_MSG_MIX_CELL
	XUCHAR                                                 cs;                                                               //校验位
	XUCHAR                                                 net_msg_tail;                                                     //网络报文尾
	_NSA3000T_NET_MSG()
	{
		net_err_num=0;
		net_msg_head1=NET_MSG_HEAD_55;
		net_msg_len=0;
		node_random_num=0;
		net_msg_type=0;
		net_msg_len=0;
		net_msg_head2=NET_MSG_HEAD_55;
		s_node_id=0;
		s_pro_id=0;
		e_node_id=0;
		e_pro_id=0;
		about_frame=0;
		append_data=0;
		order_num=0;
		pnet_buf=0;
		cs=0;
		net_msg_tail=NET_MSG_TAIL;
	}
	~_NSA3000T_NET_MSG()
	{
		net_msg_head1=NET_MSG_HEAD_55;
		net_msg_len=0;
		node_random_num=0;
		net_msg_len=0;
		net_msg_head2=NET_MSG_HEAD_55;
		s_node_id=0;
		s_pro_id=0;
		e_node_id=0;
		e_pro_id=0;
		about_frame=0;
		append_data=0;
		order_num=0;
		if (((XCHAR *)pnet_buf))
			delete (XCHAR *)pnet_buf;
		pnet_buf=XNULL;
		cs=0;
		net_msg_tail=NET_MSG_TAIL;
	}
}NSA3000T_NET_MSG;

//网络切片传输包
typedef struct _NSA3000T_LN_MSG
{
	XUCHAR                                                 net_msg_head1;                                                    //网络报文头
	XUCHAR                                                 net_msg_head2;                                                    //网络报文头
	XUINT                                                  node_random_num;                                                  //节点随机码
	XUCHAR                                                 net_msg_type;                                                     //消息属性
	XUSHORT                                                s_pro_id;                                                         //发送进程
	XUSHORT                                                e_pro_id;                                                         //目的进程
	XUCHAR                                                 about_frame;                                                      //关于包
	XUSHORT                                                append_data;                                                      //数据附加
	XUINT                                                  order_num;                                                        //命令字
	XUCHAR                                                 cs;                                                               //校验位
	XUSHORT                                                net_msg_len;                                                      //网络消息长度
	XCHAR                                                  net_buf[NSA3000T_NET_MSG_MIX_CELL];                               //数据内容
	XUCHAR                                                 net_msg_tail;                                                     //网络报文尾
	_NSA3000T_LN_MSG()
	{
		net_msg_len=0;
		node_random_num=0;
		net_msg_type=0;
		net_msg_len=0;
		net_msg_head2=NET_MSG_HEAD_55;
		s_pro_id=0;
		e_pro_id=0;
		about_frame=0;
		append_data=0;
		order_num=0;
		cs=0;
		net_msg_tail=NET_MSG_TAIL;
	}
	~_NSA3000T_LN_MSG()
	{
		net_msg_len=0;
		node_random_num=0;
		net_msg_type=0;
		net_msg_len=0;
		net_msg_head2=NET_MSG_HEAD_55;
		s_pro_id=0;
		e_pro_id=0;
		about_frame=0;
		append_data=0;
		order_num=0;
		cs=0;
		net_msg_tail=NET_MSG_TAIL;
	}
}NSA3000T_LN_MSG;

#define NET_MSG_BAG_HEAD_LENGTH                            sizeof(NSA3000T_NET_MSG)-((4+2)*sizeof(XUCHAR))
#define NET_MSG_BAG_TAIL_LENGTH                            2*sizeof(XUCHAR)

enum
{
	TRANS_NET_MSG_CMD_NO_LINK                              = 0,		// 无链路
	TRANS_NET_MSG_CMD_IDLESSSE                             ,		// 链路闲逛
	TRANS_NET_MSG_CMD_SENDED                               ,		// 已发送,等待确认
	TRANS_NET_MSG_CMD_SEND_OK                              ,		// 接收并写库成功
	TRANS_NET_MSG_CMD_WRITE_ERR                            ,		// 写库失败，停止发送后续包
	TRANS_NET_MSG_CMD_RE_SEND                              ,		// 库恢复，请继续发送
	TRANS_NET_MSG_CMD_RE_SEND_CUR_NET_MSG                  ,   	    // 链路层传输不明错误，请求重发当前包
	TRANS_NET_MSG_CMD_LINK_ASK_HEART                       ,   	    // 链路应用层激活心跳询问
	TRANS_NET_MSG_CMD_LINK_RES_HEART                       ,   	    // 链路应用层激活心跳确认
	TRANS_NET_MSG_CMD_LINK_BUSY                            ,        // 对端链路忙，请等待
};

typedef struct _NSA3000T_TRANS_NET_HEAD_MSG
{
	XUCHAR                                                 net_msg_head1;
	XUCHAR                                                 net_msg_head2;
	XUCHAR                                                 net_msg_cmd;
	_NSA3000T_TRANS_NET_HEAD_MSG()
	{
		net_msg_head1=NET_MSG_HEAD_AA;
		net_msg_head2=NET_MSG_HEAD_AA;
		net_msg_cmd=TRANS_NET_MSG_CMD_NO_LINK;
	}
	~_NSA3000T_TRANS_NET_HEAD_MSG()
	{
	}
}NSA3000T_TRANS_NET_HEAD_MSG;

#define  NSA3000T_TRANS_NET_UDP_MSG_BUF_LEN                sizeof(XCHAR)*(NET_MSG_BAG_HEAD_LENGTH+NET_MSG_BAG_TAIL_LENGTH+NSA3000T_NET_MSG_MIX_CELL)

typedef struct _NSA3000T_TRANS_UDP_NET_MSG
{
	NSA3000T_TRANS_NET_HEAD_MSG net_msg_head;
	XUSHORT                     net_msg_len;
	XUCHAR                      net_msg_cs;
	XCHAR                       net_msg_buf[NSA3000T_TRANS_NET_UDP_MSG_BUF_LEN];
	_NSA3000T_TRANS_UDP_NET_MSG()
	{	
		net_msg_len=0;
		net_msg_cs=0;		
		memset(net_msg_buf,0x0,(sizeof(XCHAR)*NSA3000T_TRANS_NET_UDP_MSG_BUF_LEN));
	}
	~_NSA3000T_TRANS_UDP_NET_MSG()
	{
	}
}NSA3000T_TRANS_UDP_NET_MSG;

#define  NSA3000T_TRANS_NET_UDP_MSG_HEAD_LEN               (XINT)(sizeof(NSA3000T_TRANS_UDP_NET_MSG))-(XINT)(NSA3000T_TRANS_NET_UDP_MSG_BUF_LEN)

#define  NSA3000T_TRANS_NET_TCP_MSG_BUF_LEN                (XINT)((NSA3000T_TRANS_SINGLE_NET_MSG_COUNT)*(sizeof(NSA3000T_LN_MSG)))

typedef struct _NSA3000T_TRANS_TCP_NET_MSG
{
	NSA3000T_TRANS_NET_HEAD_MSG                            net_msg_head;
	XUSHORT                                                net_msg_len;
	XUCHAR                                                 net_msg_cs;
	XUINT                                                  net_msg_rand_num;
	XUSHORT                                                net_msg_count;
	XCHAR                                                  net_msg_buf[NSA3000T_TRANS_NET_TCP_MSG_BUF_LEN];
	_NSA3000T_TRANS_TCP_NET_MSG()
	{
		net_msg_len=0;
		net_msg_cs=0;
		net_msg_rand_num=0;
		memset(net_msg_buf,0x0,NSA3000T_TRANS_NET_TCP_MSG_BUF_LEN);
	}
	~_NSA3000T_TRANS_TCP_NET_MSG()
	{
	}
}NSA3000T_TRANS_TCP_NET_MSG;

#define  NSA3000T_TRANS_NET_TCP_MSG_HEAD_LEN                (XINT)(sizeof(NSA3000T_TRANS_TCP_NET_MSG))-(XINT)(NSA3000T_TRANS_NET_TCP_MSG_BUF_LEN)

typedef struct _NET_TRAN_MSG
{
	XULONGLONG                                             tran_data_count;
	XULONGLONG                                             tran_time_count;
	XUINT                                                  cur_tran_rate;
	XUINT                                                  lst_tran_rate;
	NSA3000T_T                                             cur_tran_rate_t;
	XUINT                                                  max_tran_rate;
	NSA3000T_T                                             max_tran_rate_t;
	XUINT                                                  min_tran_rate;
	NSA3000T_T                                             min_tran_rate_t;
}NET_TRAN_MSG;

typedef struct _NODE_TRANS_TCP_NET_MSG
{
	NSA3000T_TRANS_TCP_NET_MSG                             trans_net_msg;
	XUSHORT                                                trans_net_msg_len;
	NSA3000T_T                                             trans_net_msg_t;
}NODE_TRANS_TCP_NET_MSG;

typedef struct _NODE_NET_LINK_MSG
{
	NODE_TRANS_TCP_NET_MSG                                *pnode_recv_trans_net_msg;
	NSA3000T_T                                             last_data_send_time;
	NSA3000T_T                                             last_data_recv_time;
	NSA3000T_T                                             conn_time;
	XUSHORT                                                link_socket;
	XUSHORT                                                conn_count;
	XUCHAR                                                 net_tran_state;
	XULONGLONG                                             send_data_count;
	XULONGLONG                                             recv_data_count;
}NODE_NET_LINK_MSG;

typedef struct _NODE_PING_MSG
{
	XBOOL                                                  node_ping_ret;
	NSA3000T_T                                             lst_ping_t;
}NODE_PING_MSG;

typedef struct _NODE_LINK_DATA_MSG
{
	NODE_NET_LINK_MSG                                      link_by_udp_msg;
	NODE_NET_LINK_MSG                                      send_link_by_tcp_msg;
	NODE_NET_LINK_MSG                                      recv_link_by_tcp_msg;
	NODE_PING_MSG                                          ping_msg;
	XUCHAR                                                 tra_net_work_state;
	XUCHAR                                                 rec_net_work_state;
	XUCHAR                                                 tra_net_tcp_listen_state;
	XUCHAR                                                 rec_net_tcp_listen_state;
	XUCHAR                                                 tra_net_udp_listen_state;
	XUCHAR                                                 rec_net_udp_listen_state;
	XCHAR                                                  ip[IP_STR_LEN];
	_NODE_LINK_DATA_MSG()
	{
	}
	~_NODE_LINK_DATA_MSG()
	{
	}
}NODE_LINK_DATA_MSG;

#pragma pack()

#include "../sys_h/NODE_PARA.h"
#pragma pack(1)

enum                                                                         //节点运行类型
{	
	NODE_RUN_TYPE_LOAD_SYS                                  = 1,		         // 加入系统
	NODE_RUN_TYPE_LOCAL_SYS                                 = 2,		         // 本地系统
	NODE_RUN_TYPE_LOCAL_NET                                 = 4,		         // 闭环节点
};

typedef struct _NODE_LINK_MSG
{
	XUINT                                                  node_id;
	XCHAR                                                  node_name[STRING_NORMAL_LEN];
	XCHAR                                                  node_byname[BYNAME_LEN];
	NODE_LINK_DATA_MSG                                     link_msg[SYS_NET_COUNT];
	NODE_PRO_MSG                                           pro_msg[PRO_MAX_COUNT];
	XUCHAR                                                 lst_send_link_no;
	XUCHAR			                                       sys_ver;										//???汾
	XUSHORT                                                node_run_type;
	NSA3000T_T                                             lst_check_t;
	NSA3000T_T                                             lst_write_t;
	NSA3000T_T                                             lst_zero_net_msg;
	NODE_REAL_STATE_MSG                                    recv_node_real_run_state;
	NODE_UDP_BROAD_SERVER_MSG                              send_node_udp_msg;
	NSA3000T_T                                             local_send_gps_broad_time;
	NSA3000T_T                                             local_send_node_real_state_time;
	XULONGLONG                                             recv_link_rand_num;
	NODE_TRANS_TCP_NET_MSG                                *pnode_send_trans_net_msg;
	_NODE_LINK_MSG()
	{
		memset(&recv_node_real_run_state,0x0,sizeof(NODE_REAL_STATE_MSG));
		memset(&send_node_udp_msg,0x0,sizeof(NODE_UDP_BROAD_SERVER_MSG));
		memset(&link_msg,0x0,sizeof(NODE_LINK_DATA_MSG)*SYS_NET_COUNT);
		memset(&node_name,0x0,STRING_NORMAL_LEN);
		memset(&node_byname,0x0,BYNAME_LEN);
		memset(pro_msg,0x0,PRO_MAX_COUNT*sizeof(NODE_PRO_MSG));
		lst_send_link_no=0;
		lst_check_t.c=0;
		lst_write_t.c=0;
		lst_zero_net_msg.c=0;
		lst_check_t.t=0;
		lst_write_t.t=0;
		lst_zero_net_msg.t=0;
		recv_link_rand_num=0;
	}
	~_NODE_LINK_MSG()
	{
		memset(&recv_node_real_run_state,0x0,sizeof(NODE_REAL_STATE_MSG));
		memset(&send_node_udp_msg,0x0,sizeof(NODE_UDP_BROAD_SERVER_MSG));
		memset(&link_msg,0x0,sizeof(NODE_LINK_DATA_MSG)*SYS_NET_COUNT);
		memset(&node_name,0x0,STRING_NORMAL_LEN);
		memset(&node_byname,0x0,BYNAME_LEN);
		memset(pro_msg,0x0,PRO_MAX_COUNT*sizeof(NODE_PRO_MSG));
		lst_send_link_no=0;
		lst_check_t.c=0;
		lst_write_t.c=0;
		lst_zero_net_msg.c=0;
		lst_check_t.t=0;
		lst_write_t.t=0;
		lst_zero_net_msg.t=0;
	}
}NODE_LINK_MSG;

typedef struct _NSA3000T_SYS_PARA
{
	LOCAL_NODE_BASIC_PARA                                  client_basic_para;
	XUCHAR                                                 used_node_count;
	XUCHAR                                                 node_idx[0XFFFF];
	NODE_LINK_MSG                                          net_link_msg[NODE_MAX_COUNT];
	_NSA3000T_SYS_PARA()
	{
	}
	~_NSA3000T_SYS_PARA()
	{
	}
}NSA3000T_SYS_PARA;

typedef struct _NODE_UDP_BROAD_SERVER_MSG_BY_BROAD_GPS_TIME_MSG
{
	NODE_UDP_BROAD_SERVER_MSG        node_udp_msg;
	XUINT                            gps_sys_time;
	XUSHORT                          gps_sys_milli;
	_NODE_UDP_BROAD_SERVER_MSG_BY_BROAD_GPS_TIME_MSG()
	{
	}
}NODE_UDP_BROAD_SERVER_MSG_BY_BROAD_GPS_TIME_MSG;


enum                                                                     //网络链路层传输协议
{	
	TRANS_NET_LINK_CLOSE                                   = 0,		         // 对方关闭
	TRANS_NET_LINK_LOW_RIGHT                               = 1,		         // 节点权限低
	TRANS_NET_LINK_TCP_OUT_TIME                            = 2,		         // 链路超时
	TRANS_NET_LINK_DISCONN                                 = 3,		         // 伪连接
	TRANS_NET_TIME_ERROR                                   = 4,              // 时钟错误
	TRANS_NET_LINK_UDP_OUT_TIME                            = 5,		         // 链路超时
	TRANS_NET_LINK_SEND_ERR                                = 6,              // 发送错误
	TRANS_NET_LINK_WORK_ERR                                = 7,              // 网卡硬件故障
	NODE_QUIT                                              = 8,              //系统退出
};

#pragma pack()

#endif // NET_H




